﻿using Socium.Domain.Tests;
using Socium.Presentation;

namespace Socium.TestEditor {
	class ClosedQuestionViewModel : FormViewModel<ClosedQuestion> {
		public ClosedQuestionViewModel(ClosedQuestion model) : base(model) {}
		public ClosedQuestionViewModel() {}

		#region Text

		string _text;

		public string Text {
			get { return _text; }
			set {
				_text = value;
				OnPropertyChanged("Text");
			}
		}

		#endregion

		#region Description

		string _description;

		public string Description {
			get { return _description; }
			set {
				_description = value;
				OnPropertyChanged("Description");
			}
		}

		#endregion

		#region Min/Max choses count

		int _min_count;

		public int MinCount {
			get { return _min_count; }
			set {
				if (value < 0) {
					value = 0;
				}

				if (MaxCount < value) {
					MaxCount = value == 0? 1 : value;
				}

				_min_count = value;
				OnPropertyChanged("MinCount");
			}
		}


		int _max_count;

		public int MaxCount {
			get { return _max_count; }
			set {
				if (value < MinCount) {
					MinCount = MaxCount;
				}

				_max_count = value;
				OnPropertyChanged("MaxCount");
			}
		}

		#endregion

		protected override void Save(ClosedQuestion model) {
			model.Text = Text;
			model.Description = _description;
			model.AnswerCountRange = new CountRange(_min_count, _max_count);
		}

		protected override string GetDisplayName(ClosedQuestion model) {
			return string.IsNullOrWhiteSpace(model.Text) ? "[Closed question]" : model.Text;
		}

		protected override string GetDefaultDisplayName() {
			return "New qusetion";
		}

		protected override ClosedQuestion CreateModel() {
			return new ClosedQuestion(Text, new CountRange(_min_count, _max_count));
		}

		protected override void Update(ClosedQuestion model) {
			Text = model.Text;
			Description = model.Description;
			MinCount = model.AnswerCountRange.Minimum;
			MaxCount = model.AnswerCountRange.Maximum;
		}

		public override bool CanSave() {
			return true;
		}
	}
}
